import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/data/controllers/notes_controller.dart';
import '../presentation/coach_notes_page/models/note_model.dart';
import '../presentation/exercises/exercises_page/models/exercise.dart';
import '../widgets/custom_text_form_field.dart';

class AddNoteBottomSheet extends StatefulWidget {
  final Exercise gymExercise;
  const AddNoteBottomSheet({super.key, required this.gymExercise});

  @override
  State<AddNoteBottomSheet> createState() => _AddNoteBottomSheetState();
}

class _AddNoteBottomSheetState extends State<AddNoteBottomSheet> {
  TextEditingController noteController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.secondary,
        borderRadius: const BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('إضافة ملاحظات المدرب', style: theme.textTheme.titleLarge),
          const SizedBox(height: 8),
          Text(widget.gymExercise.name, style: theme.textTheme.titleSmall!.copyWith(color: Colors.orange)),
          const SizedBox(height: 16),
          Expanded(
            child: CustomTextFormField(
              controller: noteController,
              textInputAction: TextInputAction.newline,
              expand: true,
              maxLines: null,
              hintText: "ادخل ملاحظاتك على تمرين اليوم",
              title: 'ملاحظات المدرب',
              fillColor: Theme.of(context).colorScheme.secondary,
              textInputType: TextInputType.multiline,
            ),
          ),
          const SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              TextButton(
                onPressed: () {
                  if (noteController.text.isNotEmpty) {
                    NoteModel note = NoteModel(
                      noteId: '-1',
                      text: noteController.text,
                      exerciseId: widget.gymExercise.exerciseId ?? "",
                    );
                    addNote(note);
                    Navigator.pop(context);
                  }
                },
                child: Text('إضافة', style: theme.textTheme.titleMedium),
              ),
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop(); // إغلاق الـ BottomSheet
                },
                child: Text('رجوع', style: theme.textTheme.titleMedium),
              ),
            ],
          ),
        ],
      ),
    );
  }

  void addNote(NoteModel note) {
    NotesController.addNote(note).then((value) {
      // معالجة النجاح
    }).catchError((e) {
      // معالجة الخطأ
    });
  }
}

// لاستخدام الـ BottomSheet:
void showAddNoteBottomSheet(BuildContext context, Exercise gymExercise) {
  showModalBottomSheet(
    context: context,
    isScrollControlled: false,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
    ),
    backgroundColor: Colors.transparent,
    builder: (context) => AddNoteBottomSheet(gymExercise: gymExercise),
  );
}
